package com.cst.emos.wx.config;

import com.cst.emos.wx.exception.EmosException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author : CST
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
    public String validExceptionHandler(Exception e){
        log.info("执行异常",e);
        if (e instanceof MethodArgumentNotValidException){
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException)e;
            //将错误信息返回给前台
            return exception.getBindingResult().getFieldError().getDefaultMessage();
        }
        else if (e instanceof EmosException){
            EmosException exception=(EmosException)e;
            return exception.getMsg();
        }
        else if (e instanceof UnauthenticatedException){
            return "你不具有相关权限";
        }
        else {
            return "后端执行异常";
        }
    }
}
